﻿using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class jfyc源代码分析 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string[] records = System.IO.File.ReadAllLines(Server.MapPath("./jfyc源代码分析/近意词列表.txt"));
        //循环采集数据
        foreach (string record in records)
        {
            string[] arr = record.Split(',');
            string keyword = arr[0];
            string url = arr[1];
            //如果有数据的话
            if (keyword.Length > 1)
            {
                分析结果代码(keyword, url);
            }
        }

    }

    /// <summary>
    /// 更新输出目录
    /// </summary>
    void docDic()
    {
        string pageIndex = "ABCDEFGHJKLMNOPQRSTWXYZ";

        List<string> results = new List<string>();
        pageIndex.ToList().ForEach((p) =>
        {
            results.Add(string.Format("{0},{1}", p, GetPageMaxNum("" + p)));
        });

        Response.Write(string.Join("<br/>", results.ToArray()));
        Response.Write("<br/>");
        foreach (var pkin in results)
        {
            //类型的页面数量
            string[] arr = pkin.Split(',');
            System.Linq.Enumerable.Range(1, int.Parse(arr[1])).ToList().ForEach((i) =>
            {
                分析源代码(arr[0], i);
                Response.Write("<br/>");
            });
        }
    }


    int GetPageMaxNum(string key)
    {
        string url = string.Format("http://tool.xdf.cn/jfyc/py_{0}.html", key);
        //找到包含这个列表的原素
        List<HtmlNode> nodes = new List<HtmlNode>();
        HtmlAgilityPack.HtmlDocument html = new HtmlAgilityPack.HtmlDocument();
        wc.Encoding = System.Text.Encoding.UTF8;
        //加载HTML数据
        html.LoadHtml(wc.DownloadString(url));
        EachAllHTMLElelents(html.DocumentNode, nodes, "class=\"pageinfo\"");
        string num = System.Text.RegularExpressions.Regex.Match(nodes[0].InnerText.Replace(" ", ""), @"共\d{1,}页").Value;
        return int.Parse(num.Replace("共", "").Replace("页", ""));
    }


    /// <summary>
    /// 用于处理url请求
    /// </summary>
    WebClient wc = new WebClient();

    void 分析源代码(string key = "a", int pageNum = 1)
    {
        string url = string.Format("http://tool.xdf.cn/jfyc/py_{0}_{1}.html", key, pageNum);
        HtmlAgilityPack.HtmlDocument html = new HtmlAgilityPack.HtmlDocument();
        wc.Encoding = System.Text.Encoding.UTF8;
        //加载HTML数据
        html.LoadHtml(wc.DownloadString(url));

        //找到包含这个列表的原素
        List<HtmlNode> nodes = new List<HtmlNode>();
        EachAllHTMLElelents(html.DocumentNode, nodes, "class=\"txt_list_03\"");
        //如果有列表记录
        if (nodes.Count > 0)
        {
            //得到当前包含列表的原素
            HtmlNode node = nodes[nodes.Count - 1];
            //定义需要链接的a原素数组
            List<HtmlNode> Atags = new List<HtmlNode>();
            //找到所有的 a原素
            EachAllHTMLElelents(node, Atags, "href=\"", false);
            Uri baseUri = new Uri(url);
            //得到所有的联接路径 是详细页面的路径
            var links = Atags.Where(p => p.Name.ToLower() == "a" && p.GetAttributeValue("href", true)).Select(p =>
                p.InnerText + "," +
                new Uri(baseUri, p.Attributes["href"].Value).ToString());
            Response.Write(string.Join("<br/>", links.ToArray()));


        }

    }

    /// <summary>
    /// 分析处理的结果代码
    /// </summary>
    /// <param name="url">路径</param>
    /// <param name="keyword">原词</param>
    void 分析结果代码(string keyword, string url = "http://tool.xdf.cn/jfyc/result_aiguozhe.html")
    {
        System.Net.WebClient client = new WebClient();
        HtmlAgilityPack.HtmlDocument html = new HtmlAgilityPack.HtmlDocument();
        wc.Encoding = System.Text.Encoding.UTF8;
        client.DownloadString(url);
        //加载HTML数据
        html.LoadHtml(wc.DownloadString(url));
        //释放资源
        client.Dispose();

        //找到包含这个列表的原素
        List<HtmlNode> nodes = new List<HtmlNode>();
        EachAllHTMLElelents(html.DocumentNode, nodes, "class=\"text_area14\"");
        //如果有列表记录
        if (nodes.Count > 0)
        {
            //得到当前包含列表的原素
            HtmlNode node = nodes[nodes.Count - 1];
            ///
            //  <p style="color:#F00"><strong>反义词：</strong>卖国贼  叛变者  </p>
            //  <p style="color:#00F"><strong>近义词：</strong>  </p>
            var chlids = node.Elements("p");
            //如果存在数据
            if (chlids.Count() >= 2)
            {


                string output = @"反义词:{0},近意词:{1},原词:{2}";

                //反义词
                // Response.Write(chlids.ElementAt(0).FirstChild.InnerHtml);
                string[] arr = chlids.ElementAt(0).LastChild.InnerText.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                //  Response.Write(string.Join(",", arr));

                //近意词
                // Response.Write(chlids.ElementAt(1).FirstChild.InnerHtml);
                string[] likearr = chlids.ElementAt(1).LastChild.InnerText.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                // Response.Write(string.Join(",", likearr));
                var d = string.Join("|", arr);
                var s = string.Join("|", likearr);
                string xxstr = string.Format(output, d, s, keyword);
                string file = Server.MapPath("./jfyc源代码分析/records.txt");
                if (System.IO.File.Exists(file))
                {
                    System.IO.File.AppendAllLines(file, new string[] { xxstr });

                }
                else
                {
                    //输出最后的结果
                    //Response.Write(xxstr);
                    System.IO.File.AppendAllLines(file, new string[] { xxstr });
                }
            }


        }


    }

    /// <summary>
    /// 遍历所有HTML原素
    /// </summary>
    /// <param name="htmlNode">html 原素</param>
    /// <param name="ContainsContent">包含的字符串内容 </param>
    /// <param name="htmlNodes">添加到的包含字符串内容的集合 </param>
    public void EachAllHTMLElelents(HtmlNode htmlNode, List<HtmlNode> htmlNodes, string ContainsContent, bool FindToBakc = true)
    {


        foreach (HtmlNode node in htmlNode.ChildNodes)
        {
            //这里采集了outerHtml可以找到原素本身
            if (node.OuterHtml.Replace("\r", "").Replace("\n", "").Replace("\t", "").Replace(" ", "").Contains(ContainsContent))
            {
                htmlNodes.Add(node);
                EachAllHTMLElelents(node, htmlNodes, ContainsContent);
                //找到直接返回
                if (FindToBakc)
                {
                    break;
                }
            }
        }
    }

}